1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.base.Converter;
22 import com.google.common.collect.testing.Helpers;
23 import com.google.common.testing.NullPointerTester;
24 import com.google.common.testing.SerializableTester;
25
26 import junit.framework.TestCase;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.List;
33 import java.util.Random;
34
35
36
37
38
39
40 @GwtCompatible(emulated = true)
41 @SuppressWarnings("cast")
42 public class ShortsTest extends TestCase {
43 private static final short[] EMPTY = {};
44 private static final short[] ARRAY1 = {(short) 1};
45 private static final short[] ARRAY234
46 = {(short) 2, (short) 3, (short) 4};
47
48 private static final short LEAST = Short.MIN_VALUE;
49 private static final short GREATEST = Short.MAX_VALUE;
50
51 private static final short[] VALUES =
52 { LEAST, (short) -1, (short) 0, (short) 1, GREATEST };
53
54 public void testHashCode() {
55 for (short value : VALUES) {
56 assertEquals(((Short) value).hashCode(), Shorts.hashCode(value));
57 }
58 }
59
60 public void testCheckedCast() {
61 for (short value : VALUES) {
62 assertEquals(value, Shorts.checkedCast((long) value));
63 }
64 assertCastFails(GREATEST + 1L);
65 assertCastFails(LEAST - 1L);
66 assertCastFails(Long.MAX_VALUE);
67 assertCastFails(Long.MIN_VALUE);
68 }
69
70 public void testSaturatedCast() {
71 for (short value : VALUES) {
72 assertEquals(value, Shorts.saturatedCast((long) value));
73 }
74 assertEquals(GREATEST, Shorts.saturatedCast(GREATEST + 1L));
75 assertEquals(LEAST, Shorts.saturatedCast(LEAST - 1L));
76 assertEquals(GREATEST, Shorts.saturatedCast(Long.MAX_VALUE));
77 assertEquals(LEAST, Shorts.saturatedCast(Long.MIN_VALUE));
78 }
79
80 private static void assertCastFails(long value) {
81 try {
82 Shorts.checkedCast(value);
83 fail("Cast to short should have failed: " + value);
84 } catch (IllegalArgumentException ex) {
85 assertTrue(value + " not found in exception text: " + ex.getMessage(),
86 ex.getMessage().contains(String.valueOf(value)));
87 }
88 }
89
90 public void testCompare() {
91 for (short x : VALUES) {
92 for (short y : VALUES) {
93
94 int expected = Short.valueOf(x).compareTo(y);
95 int actual = Shorts.compare(x, y);
96 if (expected == 0) {
97 assertEquals(x + ", " + y, expected, actual);
98 } else if (expected < 0) {
99 assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
100 actual < 0);
101 } else {
102 assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
103 actual > 0);
104 }
105 }
106 }
107 }
108
109 public void testContains() {
110 assertFalse(Shorts.contains(EMPTY, (short) 1));
111 assertFalse(Shorts.contains(ARRAY1, (short) 2));
112 assertFalse(Shorts.contains(ARRAY234, (short) 1));
113 assertTrue(Shorts.contains(new short[] {(short) -1}, (short) -1));
114 assertTrue(Shorts.contains(ARRAY234, (short) 2));
115 assertTrue(Shorts.contains(ARRAY234, (short) 3));
116 assertTrue(Shorts.contains(ARRAY234, (short) 4));
117 }
118
119 public void testIndexOf() {
120 assertEquals(-1, Shorts.indexOf(EMPTY, (short) 1));
121 assertEquals(-1, Shorts.indexOf(ARRAY1, (short) 2));
122 assertEquals(-1, Shorts.indexOf(ARRAY234, (short) 1));
123 assertEquals(0, Shorts.indexOf(
124 new short[] {(short) -1}, (short) -1));
125 assertEquals(0, Shorts.indexOf(ARRAY234, (short) 2));
126 assertEquals(1, Shorts.indexOf(ARRAY234, (short) 3));
127 assertEquals(2, Shorts.indexOf(ARRAY234, (short) 4));
128 assertEquals(1, Shorts.indexOf(
129 new short[] { (short) 2, (short) 3, (short) 2, (short) 3 },
130 (short) 3));
131 }
132
133 public void testIndexOf_arrayTarget() {
134 assertEquals(0, Shorts.indexOf(EMPTY, EMPTY));
135 assertEquals(0, Shorts.indexOf(ARRAY234, EMPTY));
136 assertEquals(-1, Shorts.indexOf(EMPTY, ARRAY234));
137 assertEquals(-1, Shorts.indexOf(ARRAY234, ARRAY1));
138 assertEquals(-1, Shorts.indexOf(ARRAY1, ARRAY234));
139 assertEquals(0, Shorts.indexOf(ARRAY1, ARRAY1));
140 assertEquals(0, Shorts.indexOf(ARRAY234, ARRAY234));
141 assertEquals(0, Shorts.indexOf(
142 ARRAY234, new short[] { (short) 2, (short) 3 }));
143 assertEquals(1, Shorts.indexOf(
144 ARRAY234, new short[] { (short) 3, (short) 4 }));
145 assertEquals(1, Shorts.indexOf(ARRAY234, new short[] { (short) 3 }));
146 assertEquals(2, Shorts.indexOf(ARRAY234, new short[] { (short) 4 }));
147 assertEquals(1, Shorts.indexOf(new short[] { (short) 2, (short) 3,
148 (short) 3, (short) 3, (short) 3 },
149 new short[] { (short) 3 }
150 ));
151 assertEquals(2, Shorts.indexOf(
152 new short[] { (short) 2, (short) 3, (short) 2,
153 (short) 3, (short) 4, (short) 2, (short) 3},
154 new short[] { (short) 2, (short) 3, (short) 4}
155 ));
156 assertEquals(1, Shorts.indexOf(
157 new short[] { (short) 2, (short) 2, (short) 3,
158 (short) 4, (short) 2, (short) 3, (short) 4},
159 new short[] { (short) 2, (short) 3, (short) 4}
160 ));
161 assertEquals(-1, Shorts.indexOf(
162 new short[] { (short) 4, (short) 3, (short) 2},
163 new short[] { (short) 2, (short) 3, (short) 4}
164 ));
165 }
166
167 public void testLastIndexOf() {
168 assertEquals(-1, Shorts.lastIndexOf(EMPTY, (short) 1));
169 assertEquals(-1, Shorts.lastIndexOf(ARRAY1, (short) 2));
170 assertEquals(-1, Shorts.lastIndexOf(ARRAY234, (short) 1));
171 assertEquals(0, Shorts.lastIndexOf(
172 new short[] {(short) -1}, (short) -1));
173 assertEquals(0, Shorts.lastIndexOf(ARRAY234, (short) 2));
174 assertEquals(1, Shorts.lastIndexOf(ARRAY234, (short) 3));
175 assertEquals(2, Shorts.lastIndexOf(ARRAY234, (short) 4));
176 assertEquals(3, Shorts.lastIndexOf(
177 new short[] { (short) 2, (short) 3, (short) 2, (short) 3 },
178 (short) 3));
179 }
180
181 public void testMax_noArgs() {
182 try {
183 Shorts.max();
184 fail();
185 } catch (IllegalArgumentException expected) {
186 }
187 }
188
189 public void testMax() {
190 assertEquals(LEAST, Shorts.max(LEAST));
191 assertEquals(GREATEST, Shorts.max(GREATEST));
192 assertEquals((short) 9, Shorts.max(
193 (short) 8, (short) 6, (short) 7,
194 (short) 5, (short) 3, (short) 0, (short) 9));
195 }
196
197 public void testMin_noArgs() {
198 try {
199 Shorts.min();
200 fail();
201 } catch (IllegalArgumentException expected) {
202 }
203 }
204
205 public void testMin() {
206 assertEquals(LEAST, Shorts.min(LEAST));
207 assertEquals(GREATEST, Shorts.min(GREATEST));
208 assertEquals((short) 0, Shorts.min(
209 (short) 8, (short) 6, (short) 7,
210 (short) 5, (short) 3, (short) 0, (short) 9));
211 }
212
213 public void testConcat() {
214 assertTrue(Arrays.equals(EMPTY, Shorts.concat()));
215 assertTrue(Arrays.equals(EMPTY, Shorts.concat(EMPTY)));
216 assertTrue(Arrays.equals(EMPTY, Shorts.concat(EMPTY, EMPTY, EMPTY)));
217 assertTrue(Arrays.equals(ARRAY1, Shorts.concat(ARRAY1)));
218 assertNotSame(ARRAY1, Shorts.concat(ARRAY1));
219 assertTrue(Arrays.equals(ARRAY1, Shorts.concat(EMPTY, ARRAY1, EMPTY)));
220 assertTrue(Arrays.equals(
221 new short[] {(short) 1, (short) 1, (short) 1},
222 Shorts.concat(ARRAY1, ARRAY1, ARRAY1)));
223 assertTrue(Arrays.equals(
224 new short[] {(short) 1, (short) 2, (short) 3, (short) 4},
225 Shorts.concat(ARRAY1, ARRAY234)));
226 }
227
228 @GwtIncompatible("Shorts.toByteArray")
229 public void testToByteArray() {
230 assertTrue(Arrays.equals(
231 new byte[] {0x23, 0x45}, Shorts.toByteArray((short) 0x2345)));
232 assertTrue(Arrays.equals(
233 new byte[] {(byte) 0xFE, (byte) 0xDC},
234 Shorts.toByteArray((short) 0xFEDC)));
235 }
236
237 @GwtIncompatible("Shorts.fromByteArray")
238 public void testFromByteArray() {
239 assertEquals((short) 0x2345,
240 Shorts.fromByteArray(new byte[] {0x23, 0x45}));
241 assertEquals((short) 0xFEDC, Shorts.fromByteArray(
242 new byte[] {(byte) 0xFE, (byte) 0xDC}));
243
244 try {
245 Shorts.fromByteArray(new byte[] {0x01});
246 fail();
247 } catch (IllegalArgumentException expected) {
248 }
249 }
250
251 @GwtIncompatible("Shorts.fromBytes")
252 public void testFromBytes() {
253 assertEquals((short) 0x2345, Shorts.fromBytes((byte) 0x23, (byte) 0x45));
254 assertEquals((short) 0xFEDC, Shorts.fromBytes((byte) 0xFE, (byte) 0xDC));
255 }
256
257 @GwtIncompatible("Shorts.fromByteArray, Shorts.toByteArray")
258 public void testByteArrayRoundTrips() {
259 Random r = new Random(5);
260 byte[] b = new byte[Shorts.BYTES];
261
262
263 for (int i = 0; i < 10000; i++) {
264 short num = (short) r.nextInt();
265 assertEquals(num, Shorts.fromByteArray(Shorts.toByteArray(num)));
266
267 r.nextBytes(b);
268 assertTrue(Arrays.equals(b,
269 Shorts.toByteArray(Shorts.fromByteArray(b))));
270 }
271 }
272
273 public void testEnsureCapacity() {
274 assertSame(EMPTY, Shorts.ensureCapacity(EMPTY, 0, 1));
275 assertSame(ARRAY1, Shorts.ensureCapacity(ARRAY1, 0, 1));
276 assertSame(ARRAY1, Shorts.ensureCapacity(ARRAY1, 1, 1));
277 assertTrue(Arrays.equals(
278 new short[] {(short) 1, (short) 0, (short) 0},
279 Shorts.ensureCapacity(ARRAY1, 2, 1)));
280 }
281
282 public void testEnsureCapacity_fail() {
283 try {
284 Shorts.ensureCapacity(ARRAY1, -1, 1);
285 fail();
286 } catch (IllegalArgumentException expected) {
287 }
288 try {
289
290 Shorts.ensureCapacity(ARRAY1, 1, -1);
291 fail();
292 } catch (IllegalArgumentException expected) {
293 }
294 }
295
296 public void testJoin() {
297 assertEquals("", Shorts.join(",", EMPTY));
298 assertEquals("1", Shorts.join(",", ARRAY1));
299 assertEquals("1,2", Shorts.join(",", (short) 1, (short) 2));
300 assertEquals("123",
301 Shorts.join("", (short) 1, (short) 2, (short) 3));
302 }
303
304 public void testLexicographicalComparator() {
305 List<short[]> ordered = Arrays.asList(
306 new short[] {},
307 new short[] {LEAST},
308 new short[] {LEAST, LEAST},
309 new short[] {LEAST, (short) 1},
310 new short[] {(short) 1},
311 new short[] {(short) 1, LEAST},
312 new short[] {GREATEST, GREATEST - (short) 1},
313 new short[] {GREATEST, GREATEST},
314 new short[] {GREATEST, GREATEST, GREATEST});
315
316 Comparator<short[]> comparator = Shorts.lexicographicalComparator();
317 Helpers.testComparator(comparator, ordered);
318 }
319
320 @GwtIncompatible("SerializableTester")
321 public void testLexicographicalComparatorSerializable() {
322 Comparator<short[]> comparator = Shorts.lexicographicalComparator();
323 assertSame(comparator, SerializableTester.reserialize(comparator));
324 }
325
326 @GwtIncompatible("SerializableTester")
327 public void testStringConverterSerialization() {
328 SerializableTester.reserializeAndAssert(Shorts.stringConverter());
329 }
330
331 public void testToArray() {
332
333 List<Short> none = Arrays.<Short>asList();
334 assertTrue(Arrays.equals(EMPTY, Shorts.toArray(none)));
335
336 List<Short> one = Arrays.asList((short) 1);
337 assertTrue(Arrays.equals(ARRAY1, Shorts.toArray(one)));
338
339 short[] array = {(short) 0, (short) 1, (short) 3};
340
341 List<Short> three = Arrays.asList((short) 0, (short) 1, (short) 3);
342 assertTrue(Arrays.equals(array, Shorts.toArray(three)));
343
344 assertTrue(Arrays.equals(array, Shorts.toArray(Shorts.asList(array))));
345 }
346
347 public void testToArray_threadSafe() {
348 for (int delta : new int[] { +1, 0, -1 }) {
349 for (int i = 0; i < VALUES.length; i++) {
350 List<Short> list = Shorts.asList(VALUES).subList(0, i);
351 Collection<Short> misleadingSize =
352 Helpers.misleadingSizeCollection(delta);
353 misleadingSize.addAll(list);
354 short[] arr = Shorts.toArray(misleadingSize);
355 assertEquals(i, arr.length);
356 for (int j = 0; j < i; j++) {
357 assertEquals(VALUES[j], arr[j]);
358 }
359 }
360 }
361 }
362
363 public void testToArray_withNull() {
364 List<Short> list = Arrays.asList((short) 0, (short) 1, null);
365 try {
366 Shorts.toArray(list);
367 fail();
368 } catch (NullPointerException expected) {
369 }
370 }
371
372 public void testToArray_withConversion() {
373 short[] array = {(short) 0, (short) 1, (short) 2};
374
375 List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
376 List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
377 List<Integer> ints = Arrays.asList(0, 1, 2);
378 List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
379 List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
380 List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
381
382 assertTrue(Arrays.equals(array, Shorts.toArray(bytes)));
383 assertTrue(Arrays.equals(array, Shorts.toArray(shorts)));
384 assertTrue(Arrays.equals(array, Shorts.toArray(ints)));
385 assertTrue(Arrays.equals(array, Shorts.toArray(floats)));
386 assertTrue(Arrays.equals(array, Shorts.toArray(longs)));
387 assertTrue(Arrays.equals(array, Shorts.toArray(doubles)));
388 }
389
390 public void testAsList_isAView() {
391 short[] array = {(short) 0, (short) 1};
392 List<Short> list = Shorts.asList(array);
393 list.set(0, (short) 2);
394 assertTrue(Arrays.equals(new short[] {(short) 2, (short) 1}, array));
395 array[1] = (short) 3;
396 assertEquals(Arrays.asList((short) 2, (short) 3), list);
397 }
398
399 public void testAsList_toArray_roundTrip() {
400 short[] array = { (short) 0, (short) 1, (short) 2 };
401 List<Short> list = Shorts.asList(array);
402 short[] newArray = Shorts.toArray(list);
403
404
405 list.set(0, (short) 4);
406 assertTrue(Arrays.equals(
407 new short[] { (short) 0, (short) 1, (short) 2 }, newArray));
408 newArray[1] = (short) 5;
409 assertEquals((short) 1, (short) list.get(1));
410 }
411
412
413 public void testAsList_subList_toArray_roundTrip() {
414 short[] array = { (short) 0, (short) 1, (short) 2, (short) 3 };
415 List<Short> list = Shorts.asList(array);
416 assertTrue(Arrays.equals(new short[] { (short) 1, (short) 2 },
417 Shorts.toArray(list.subList(1, 3))));
418 assertTrue(Arrays.equals(new short[] {},
419 Shorts.toArray(list.subList(2, 2))));
420 }
421
422 public void testAsListEmpty() {
423 assertSame(Collections.emptyList(), Shorts.asList(EMPTY));
424 }
425
426 @GwtIncompatible("NullPointerTester")
427 public void testNulls() {
428 new NullPointerTester().testAllPublicStaticMethods(Shorts.class);
429 }
430
431 public void testStringConverter_convert() {
432 Converter<String, Short> converter = Shorts.stringConverter();
433 assertEquals((Short) (short) 1, converter.convert("1"));
434 assertEquals((Short) (short) 0, converter.convert("0"));
435 assertEquals((Short) (short) (-1), converter.convert("-1"));
436 assertEquals((Short) (short) 255, converter.convert("0xff"));
437 assertEquals((Short) (short) 255, converter.convert("0xFF"));
438 assertEquals((Short) (short) (-255), converter.convert("-0xFF"));
439 assertEquals((Short) (short) 255, converter.convert("#0000FF"));
440 assertEquals((Short) (short) 438, converter.convert("0666"));
441 }
442
443 public void testStringConverter_convertError() {
444 try {
445 Shorts.stringConverter().convert("notanumber");
446 fail();
447 } catch (NumberFormatException expected) {
448 }
449 }
450
451 public void testStringConverter_nullConversions() {
452 assertNull(Shorts.stringConverter().convert(null));
453 assertNull(Shorts.stringConverter().reverse().convert(null));
454 }
455
456 public void testStringConverter_reverse() {
457 Converter<String, Short> converter = Shorts.stringConverter();
458 assertEquals("1", converter.reverse().convert((short) 1));
459 assertEquals("0", converter.reverse().convert((short) 0));
460 assertEquals("-1", converter.reverse().convert((short) -1));
461 assertEquals("255", converter.reverse().convert((short) 0xff));
462 assertEquals("255", converter.reverse().convert((short) 0xFF));
463 assertEquals("-255", converter.reverse().convert((short) -0xFF));
464 assertEquals("438", converter.reverse().convert((short) 0666));
465 }
466
467 @GwtIncompatible("NullPointerTester")
468 public void testStringConverter_nullPointerTester() throws Exception {
469 NullPointerTester tester = new NullPointerTester();
470 tester.testAllPublicInstanceMethods(Shorts.stringConverter());
471 }
472 }